package com.cybertron.ironhide.search.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cybertron.ironhide.search.domain.Member;
import com.cybertron.ironhide.search.service.MemberService;
import com.cybertron.ironhide.search.util.CommonUtil;

@Controller
public class SignUpController {

	@Autowired(required = false)
	private MemberService memberService;

	@RequestMapping(value = "sign", method = RequestMethod.POST)
	public String sign(@RequestParam("email") String email, Model model) {
		Boolean isAgain = false;
		Member member = memberService.selectByEmail(email);
		if (member != null) {
			isAgain = true;
			member.setModifyDate(new Date());
			memberService.updateByPrimaryKeySelective(member);
		} else {
			member = new Member();
			member.setId(CommonUtil.getUUID());
			member.setEmail(email);
			member.setModifyDate(new Date());
			member.setCreateDate(new Date());
			memberService.insertSelective(member);
		}
		model.addAttribute("email", email);
		model.addAttribute("isAgain", isAgain);
		return "sign_up";
	}
}
